﻿package pw.archipelag.loaderGame.panel {
	import flash.display.MovieClip;
	import flash.display.Shape;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinTf;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinHelp;
	import flash.geom.Rectangle;
	
	public class PanelHelp extends PanelAbstractLabel {
		
		private var classMcHelp: Class;
		private var mcHelp: MovieClip;
		private var maskMcHelp: Shape;
		private var skinHelp: SkinHelp;
		private var basePosY: Number;
		private var _width: Number;
		private var _height: Number;
		
		public function PanelHelp(classMcHelp: Class): void {
			this.classMcHelp = classMcHelp;
			super("POMOC");
			this.initMcHelp();
		}
		
		private function initMcHelp(): void {
			if (this.classMcHelp)
				this.mcHelp = new this.classMcHelp();
			else {
				this.mcHelp = new MovieClip();
				this.mcHelp.graphics.beginFill(0xDDDDDD, 1);
				this.mcHelp.graphics.drawRect(0, 0, 2000, 1000);
				this.mcHelp.graphics.endFill();
			}
			this.createMaskMcHelp();
			this.mcHelp.mask = this.maskMcHelp;
			this.addChild(this.mcHelp);
			this.stop();
		}
		
		private function createMaskMcHelp(): void {
			this.maskMcHelp = new Shape();
			this.drawMaskMcHelp(this.mcHelp.width, this.mcHelp.height);
			this.addChild(this.maskMcHelp);
		}
		
		private function drawMaskMcHelp(width: Number, height: Number): void {
			this.maskMcHelp.graphics.clear();
			this.maskMcHelp.graphics.beginFill(0xFFFFFF, 1);
			this.maskMcHelp.graphics.drawRect(0, 0, width, height);
			this.maskMcHelp.graphics.endFill();
		}

		public function draw(skinTfLabelTitles: SkinTf, skinHelp: SkinHelp): void {
			this.drawToTfLabel(skinTfLabelTitles);
			this.basePosY =	this.mcHelp.y = this.maskMcHelp.y = this.tfLabel.y + this.tfLabel.height + PanelAbstractBg.MARGIN_Y_ELEMENTS / 2;
			if (skinHelp.dimensionMc) this.drawMaskMcHelp(skinHelp.dimensionMc.width, skinHelp.dimensionMc.height);
			this.skinHelp = skinHelp;
		}
		
		override public function get width(): Number {
			return this._width;
		}
		
		override public function set width(value: Number): void {
			this._width = this.maskMcHelp.width = value;
			this.setScaleAndPositionMcHelp();
		}
		
		override public function get height(): Number {
			return this._height;
		}
		
		override public function set height(value: Number): void {
			this._height = this.maskMcHelp.height = value - this.basePosY;
			this.setScaleAndPositionMcHelp();
		}
		
		private function getDimensionMcHelp(): Rectangle {
			var dimension: Rectangle = this.skinHelp.dimensionMc ? this.skinHelp.dimensionMc.clone() : new Rectangle(0, 0);
			if (!dimension.width) dimension.width = this._width - PanelAbstractBg.MARGIN_X_ELEMENTS;
			if (!dimension.height) dimension.height = this._height  - PanelAbstractBg.MARGIN_Y_ELEMENTS;
			return dimension;
		}
		
		private function getScaleMcHelp(dimension: Rectangle): Number {
			return Math.min(dimension.width / this.maskMcHelp.width, dimension.height / this.maskMcHelp.height);
		}
		
		private function setScaleAndPositionMcHelp(): void {
			var dimensionMcHelp: Rectangle = this.getDimensionMcHelp();
			this.maskMcHelp.scaleX = this.maskMcHelp.scaleY = 1;
			this.mcHelp.scaleX = this.mcHelp.scaleY = this.maskMcHelp.scaleX = this.maskMcHelp.scaleY = this.getScaleMcHelp(dimensionMcHelp);
			this.mcHelp.x = this.maskMcHelp.x = (this._width - this.maskMcHelp.width) / 2;
			this.mcHelp.y = this.maskMcHelp.y = this.basePosY + (this._height - this.maskMcHelp.height) / 2;
		}
		
		override internal function initShow():void {
			this.mcHelp.gotoAndStop(1);
		}
		
		override internal function start(): void {
			this.mcHelp.gotoAndPlay(1);
		}
		
		override internal function stop(): void {
			this.mcHelp.stop();
		}
		
		override public function destroy(): void {
			this.stop();
			this.removeChild(this.maskMcHelp);
			this.removeChild(this.mcHelp);
			super.destroy();
		
		}
		
	}

}